package Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.*;
import Entity.*;

public class ListBankServlet extends HttpServlet {

	private ListBankDao listbankdao = new ListBankDao();
	private UserListDao userdao = new UserListDao();
	private int UserCode = 0;
	private String UserName = "";
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		
		String path = "pages/List_Bank_Select.jsp";
		boolean ac = false;
		if(method.equals("list")){
			System.out.println("list");
			int pages_rows = 17;//每页显示的行数
			String begin_year ="";
			String begin_month = "";
			String begin_day = "";
			String end_year = "";
			String end_month = "";
			String end_day = "";
			//获得输入帐号的代号
			int code = Integer.parseInt(request.getParameter("name"));

			//获得账户列表（用于代号转中文）
			List<UserListEntity>list_user = userdao.select();
			request.setAttribute("list_user", list_user);
			//如果该账户没有数据则不再执行
			if(listbankdao.select(code).isEmpty()){
				request.setAttribute("begin_year_select", begin_year);
				request.setAttribute("begin_month_select", begin_month);
				request.setAttribute("begin_day_select", begin_day);
				
				request.setAttribute("end_year_select", end_year);
				request.setAttribute("end_month_select", end_month);
				request.setAttribute("end_day_select", end_day);
				request.setAttribute("name", request.getParameter("code"));
				request.getRequestDispatcher(path).forward(request, response);
				return;
			}
			
			//请求的页码
			int page = 1;
			if(request.getParameter("pages_num")!=""){
				page = Integer.parseInt(request.getParameter("pages_num"));
			}
			//如果在网页中的不到查询的时间，那么就显示全部的搜索结果
			begin_year = request.getParameter("begin_year");
			begin_month = request.getParameter("begin_month");
			begin_day = request.getParameter("begin_day");
			end_year = request.getParameter("end_year");
			end_month = request.getParameter("end_month");
			end_day = request.getParameter("end_day");
			if(begin_year==null && end_year==null && end_day == null){
				begin_year ="";
				begin_month = "";
				begin_day = "";
				end_year = "";
				end_month = "";
				end_day = "";
			}
			//1.如果用户第一次查询，时间条件为所要查询账户的第一条与最后一条数据的时间段
			//2.如果用户翻页，则判断时间条件已有之前填写的时间段
			//3.如果用户查询另外账户，则判断当前查询账号的CODE与之前查询账号的CODE不同，则时间段从新读取为第一条
			if(begin_year.equals("") || begin_month.equals("") || begin_day.equals("")
					|| end_year.equals("") || end_month.equals("") || end_day.equals("") || UserCode!=code){
				//获得查询数据的 最早时间和最晚时间
				String[] begin_times = new String[]{};
				String begin_time = listbankdao.select_begin_time(code);
				if(!begin_time.equals("")){
					begin_times = begin_time.split(" ");
					String[] begin_date = new String[]{};
					begin_date = begin_times[0].split("-");
					begin_year = begin_date[0];
					begin_month = begin_date[1];
					
					begin_day = begin_date[2];
					System.out.println("begin_day"+begin_day);
					
					String[] end_times = new String[]{};
					String end_time = listbankdao.select_end_time(code);
					end_times = end_time.split(" ");
					String[] end_date = new String[]{};
					end_date = end_times[0].split("-");
					end_year = end_date[0];
					end_month = end_date[1];
					end_day = end_date[2];
					System.out.println("end_day"+end_day);
//					System.out.println("end:"+end_date[0]+"--"+end_date[1]+"--"+end_date[2]);
				}else {
					request.getRequestDispatcher(path).forward(request, response);
				}
			}else{
				if(Integer.parseInt(begin_month) < 10){
					begin_month="0"+begin_month;
				}
				if(Integer.parseInt(end_month) < 10){
					end_month="0"+end_month;
				}
				if(Integer.parseInt(begin_day) < 10){
					begin_day="0"+begin_day;
				}
				if(Integer.parseInt(end_day) < 10){
					end_day="0"+end_day;
				}
			}
			
			String begin_time = begin_year+"-"+begin_month+"-"+begin_day+" 00:00:00";
			String end_time = end_year+"-"+end_month+"-"+end_day+" 23:59:59";
			fenye(request,response,pages_rows,code,page,begin_time,end_time);
			
			if(Integer.parseInt(begin_month) < 10){
				begin_month=begin_month.charAt(1)+"";
			}
			if(Integer.parseInt(end_month) < 10){
				end_month=""+end_month.charAt(1);
			}
			if(Integer.parseInt(begin_day) < 10){
				begin_day=""+begin_day.charAt(1);
			}
			if(Integer.parseInt(end_day) < 10){
				end_day=""+end_day.charAt(1);
			}
			
			System.out.println("begin_day_01"+begin_day);
			//获得当前的年 月 日
			request.setAttribute("begin_year_select", begin_year);
			request.setAttribute("begin_month_select", begin_month);
			request.setAttribute("begin_day_select", begin_day);
			
			request.setAttribute("end_year_select", end_year);
			request.setAttribute("end_month_select", end_month);
			request.setAttribute("end_day_select", end_day);
			
			
			UserCode = code;
		}else if(method.equals("hl")){
			List<UserListEntity>list_user = userdao.select();
			request.setAttribute("list_user", list_user);
		}else if(method.equals("print")){
			int code = Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("name");
			String end_time = request.getParameter("e_time") + " 23:59:59";
			String begin_time = request.getParameter("b_time") + " 00:00:00";
			List<ListBankEntity>list = listbankdao.select_Print_time(code, begin_time, end_time);
			if(list.isEmpty()){
				request.setAttribute("name", "没有数据");
			}else {
				request.setAttribute("name", list.get(0).getName());
			}
			int pages = list.size() / 17;
			if(list.size() % 17 > 0){
				pages+=1;
			}
			if(pages == 0){
				pages = 1;
			}
			request.setAttribute("rows", list.size());
			request.setAttribute("pages", pages);
			request.setAttribute("list", list);
			request.setAttribute("end_time", request.getParameter("e_time"));
			request.setAttribute("begin_time", request.getParameter("b_time"));
			path = "pages/PrintView.jsp";
		}else if(method.equals("toedit")){
//			location.href="List_Bank_Servlet?method=toedit&id="+id+"&b_time="+begin_time+"&e_time="+end_time;
			int code = Integer.parseInt(request.getParameter("id"));
			String end_time = request.getParameter("e_time") + " 23:59:59";
			String begin_time = request.getParameter("b_time") + " 00:00:00";
			List<ListBankEntity>list = listbankdao.select_Print_time(code, begin_time, end_time);
			if(list.isEmpty()){
				request.setAttribute("name", "没有数据");
			}else {
				request.setAttribute("name", list.get(0).getName());
			}
			int pages = list.size() / 17;
			if(list.size() % 17 > 0){
				pages+=1;
			}
			if(pages == 0){
				pages = 1;
			}
			request.setAttribute("code", code);
			request.setAttribute("rows", list.size());
			request.setAttribute("pages", pages);
			request.setAttribute("list", list);
			request.setAttribute("end_time", request.getParameter("e_time"));
			request.setAttribute("begin_time", request.getParameter("b_time"));
			path = "pages/EditView.jsp";
		}else if(method.equals("edit")){	
			//List_BankServlet?method=edit&project= &id= &pay_total= &surplus= &end_time= &begin_time
			int project = Integer.parseInt(request.getParameter("project"));
			int id  = Integer.parseInt(request.getParameter("id"));
			double pay_total = Double.parseDouble(request.getParameter("total"));
			double surplus = Double.parseDouble(request.getParameter("surplus"));
			listbankdao.update_Bank(project, id, pay_total, surplus);
			//跳转至toedit()所需的参数
			int code = Integer.parseInt(request.getParameter("code"));
			String end_time = request.getParameter("end_time");
			String begin_time = request.getParameter("begin_time");
			path = "List_Bank_Servlet?method=toedit&id="+code+"&b_time="+begin_time+"&e_time="+end_time;
		}else if(method.equals("del")){
			//List_BankServlet?method=del&project=&id=&end_time=&begin_time=
			int project = Integer.parseInt(request.getParameter("project"));
			int id  = Integer.parseInt(request.getParameter("id"));
			int num = Integer.parseInt(request.getParameter("num"));
			//1.查出要删除的num，和最后一个的num
			//2.从比它大1的num开始，依次减1
			listbankdao.del_Bank(project, id);
			int max_num = listbankdao.sel_max_num(project);
//			num = 106
			for(int i = num;i < max_num;i++){
				listbankdao.update_ever_num(project, i+1, i);
			}
//				106 1.107 106 2.108 107 
//				107
//				108
			//跳转至toedit()所需的参数
			int code = Integer.parseInt(request.getParameter("code"));
			String end_time = request.getParameter("end_time");
			String begin_time = request.getParameter("begin_time");
			path = "List_Bank_Servlet?method=toedit&id="+code+"&b_time="+begin_time+"&e_time="+end_time;
		}else if(method.equals("back")){
			int code = Integer.parseInt("code");
			path="List_Bank_Servlet?method=list&name="+code;
		}
		if(ac){
			response.sendRedirect(path);
		}else{
			request.getRequestDispatcher(path).forward(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	//
	public void fenye(HttpServletRequest request,HttpServletResponse response,
			int pages_rows,int code,int page,String begin_time,String end_time){
		//得出数据总行数
		int rows = listbankdao.select_pages_num(code,begin_time,end_time);
		
		//得出数据页数
		int pages = rows / pages_rows;
		if(rows % pages_rows > 0 && pages!=0){
			pages+=1;
		}
		if(pages == 0){
			pages = 1;
		}
		request.setAttribute("pages", pages);
		if(page != 0){
			int begin = (page-1) * pages_rows;
			//获得该页码内的显示数据
//			System.out.println("begin:"+begin);
//			System.out.println("pages_rows:"+pages_rows);
//			System.out.println("code:"+code);
//			System.out.println("begin_time:"+begin_time);
//			System.out.println("end_time:"+end_time);
			
			List<ListBankEntity>list = listbankdao.select_pages(begin, pages_rows, code,begin_time,end_time);
			if(!list.isEmpty()){
				request.setAttribute("list", list);
				request.setAttribute("code", code);
				request.setAttribute("pages_num", page);
				request.setAttribute("name", list.get(0).getName());
				UserName = list.get(0).getName();
			}else {
				request.setAttribute("name", UserName);
			}
		}
	}
}
